package com.wft;

import java.util.Arrays;
import java.util.Collections;

public class Solution761 {
    public static void main(String[] args) {
        int[] a = new int[]{3, 1,10};
        System.out.println(smallestRangeI(a, 4));

        int[] b = new int[]{2,7,2};
        System.out.println(smallestRangeI(b, 1));
    }

    public static int smallestRangeI(int[] A, int K) {
        if (A == null || A.length == 1) {
            return 0;
        }

        int max = A[0];
        int min = A[0];
        for (int i = 1; i < A.length; i++) {
            if (A[i] > max) {
                max = A[i];
            }
            if (A[i] < min) {
                min = A[i];
            }
        }

        if (max - min > K * 2   ) {
            return (max - K) - (min + K);
        } else {
            return 0;
        }
    }
}
